package org.example.controller;


import dto.StandardQuaryDTO;

import entity.QualityStandard;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.result.PageResult;
import org.example.result.Result;
import org.example.service.ProcessService;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Tag(name = "工艺工程师控制层")
@RequestMapping("/process-engineer")
@RestController
@RequiredArgsConstructor
public class ProcessController {

    private final ProcessService processService;

    /**
     * 分页查询原材料信息
     * @param pageNum
     * @param pageSize
     * @param standardId
     * @param standardType
     * @return
     */
    @GetMapping("/raw-material-standard")
    public Result QueryRawMaterialStandards(
            @RequestParam(name = "pageNum") Integer pageNum,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "standardId", required = false) String standardId,
            @RequestParam(name = "standardType", required = false) String standardType,
            @RequestParam(name = "itemName", required = false) String itemName
    ) {
        log.info("查询原材料标准：pageNum = {}, pageSize = {}, standardId = {}, standardType = {}, itemName = {}", pageNum, pageSize, standardId, standardType, itemName);
        StandardQuaryDTO standardQuaryDTO = StandardQuaryDTO.builder()
                .pageNum(pageNum)
                .pageSize(pageSize)
                .standardId(standardId)
                .StandardType(standardType)
                .itemName(itemName)
                .build();
        PageResult pageResult = processService.queryRawMaterialStandards(standardQuaryDTO);
        return Result.success(pageResult);
    }

    /**
     * 分页查询信息
     * @param pageNum
     * @param pageSize
     * @param standardId
     * @param standardType
     * @return
     */
    @GetMapping("/semi-finished-Standards")
    public Result QuerySemiFinishedStandards(
            @RequestParam(name = "pageNum") Integer pageNum,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "standardId", required = false) String standardId,
            @RequestParam(name = "standardType", required = false) String standardType,
            @RequestParam(name = "itemName", required = false) String itemName
    ) {
        log.info("查询半成品标准：pageNum = {}, pageSize = {}, standardId = {}, itemName = {}", pageNum, pageSize, standardId, standardType,itemName);
        StandardQuaryDTO standardQuaryDTO = StandardQuaryDTO.builder()
                .pageNum(pageNum)
                .pageSize(pageSize)
                .standardId(standardId)
                .StandardType(standardType)
                .itemName(itemName)
                .build();
        PageResult pageResult = processService.querySemiFinishedStandards(standardQuaryDTO);
        return Result.success(pageResult);
    }

    /**
     * 分页查询信息
     * @param pageNum
     * @param pageSize
     * @param standardId
     * @param standardType
     * @return
     */
    @GetMapping("/finished-Standards")
    public Result QueryFinishedStandards(
            @RequestParam(name = "pageNum") Integer pageNum,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "standardId", required = false) String standardId,
            @RequestParam(name = "standardType", required = false) String standardType,
            @RequestParam(name = "itemName", required = false) String itemName
    ) {
        log.info("查询成品标准：pageNum = {}, pageSize = {}, standardId = {}, standardType = {}, itemName = {}", pageNum, pageSize, standardId, standardType,itemName);
        StandardQuaryDTO standardQuaryDTO = StandardQuaryDTO.builder()
                .pageNum(pageNum)
                .pageSize(pageSize)
                .standardId(standardId)
                .StandardType(standardType)
                .itemName(itemName)
                .build();
        PageResult pageResult = processService.queryFinishedStandards(standardQuaryDTO);
        return Result.success(pageResult);
    }



    /**
     * 添加标准
     * @param qualityStandard
     * @return
     */
    @PostMapping("/addStandard")
    @Operation(description = "添加标准")
    public Result addStandard(@RequestBody QualityStandard qualityStandard){
        log.info("添加质量标准：qualityStandard = {}", qualityStandard);
        processService.addStandard(qualityStandard);
        return Result.success();
    }

    /**
     * 更新标准
     * @param qualityStandard
     * @return
     */
    @Operation(description = "更新标准")
    @PutMapping("/updateStandard")
    public Result updateStandard(@RequestBody QualityStandard qualityStandard){
        processService.updateStandard(qualityStandard);
        return Result.success();
    }

    /**
     * 删除标准
     * @param standardId
     * @param standardStatus
     * @return
     */
    @Operation(description = "删除标准")
    @DeleteMapping("/deleteStandard")
    public Result deleteStandard(
            @RequestParam Integer standardId,
            @RequestParam String standardStatus
    ){
        log.info("删除标准：standardId = {}, standardStatus = {}", standardId, standardStatus);
        if (processService.deleteStandard(standardId, standardStatus)) {
            return Result.success();
        }else {
            return Result.error("该标准已启用，不能删除");
        }
    }

    /**
     * 更新标准状态
     * @return
     */
    @Operation(description = "更新标准状态")
    @PutMapping("/updateStandardStatus")
    public Result updateStandardStatus(@RequestBody QualityStandard qualityStandard){
        processService.updateStandardStatus(qualityStandard);
        return Result.success();
    }

}
